package com.educate.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.baomidou.mybatisplus.extension.api.R;
import com.educate.commonutils.ResultOne;
import com.educate.servicebase.exceptionhander.EducateException;
import com.educate.vod.service.VodService;
import com.educate.vod.utils.ConstantVodUtils;
import com.educate.vod.utils.InitVodClient;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {
    @Autowired
    private VodService vodService;
    //上传视频到阿里云
    @PostMapping("uploadAliYunVideo")
    public ResultOne uploadAliYunVideo(MultipartFile file) {
        String videoId=vodService.uploadVideoAly(file);
        return ResultOne.ok().data("videoId",videoId);
    }
    //根据视频id删除阿里云视频
    @DeleteMapping("removeAllVideo/{id}")
    public ResultOne removeAllVideo(@PathVariable String id){
        try{
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频的request对象
            DeleteVideoRequest request=new DeleteVideoRequest();
            //向request中设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return ResultOne.ok();
        }catch (Exception e){
            e.printStackTrace();
            throw new EducateException(20001,"删除视频失败");
        }
    }
    //删除多个阿里云视频的方法
    @DeleteMapping("delete-Bath")
    public ResultOne deleteBath(@RequestParam("videoList") List<String> videoList){
        vodService.removeMoreAlyVideo(videoList);
        return ResultOne.ok();
    }
    //根据视频id获得视频的凭证
    @GetMapping("getPlayAuth/{id}")
    public ResultOne getPlayAuth(@PathVariable String id){
        try{
            //创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取凭证的request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse reponse = client.getAcsResponse(request);
            String playAuth = reponse.getPlayAuth();
            return ResultOne.ok().data("playAuth",playAuth);
        }catch (Exception e){
            throw new EducateException(20001,"获取视频凭证失败");
        }
    }
}
